import java.util.ArrayList;
import java.util.List;

public class FindAnagrams {
    public List<Integer> findAnagrams(String s, String p) {
        int len1 = s.length(), len2 = p.length();
        int[] hash1 = new int[26];
        int[] hash2 = new int[26];
        for (char c : p.toCharArray()) {
            hash2[c-'a']++;
        }
        int count = 0;
        // count记录窗口中有效的字母的个数（窗口中某一个字母的个数大于p中对应字母的个数）
        int left = 0, right = 0;
        List<Integer> ret = new ArrayList<>();
        for (;right < len1; right++) {
            char c = s.charAt(right);
            if (++hash1[c-'a'] <= hash2[c-'a']) count++;
            if (right - left + 1 > len2) {
                if (hash1[s.charAt(left) - 'a']-- <= hash2[s.charAt(left++) - 'a']) count--;
            }
            if (count == len2) ret.add(left);
        }
        return ret;
    }

    public static void main(String[] args) {
        FindAnagrams f = new FindAnagrams();
        String s = "cbaebabacd";
        String p = "abc";
        f.findAnagrams(s, p);
    }
}
